package com.woniu.controller;

import com.woniu.service.PermissionService;
import com.woniu.utils.ResponseEnum;
import com.woniu.utils.ResponseUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/per")
public class PermissionController {
    @Resource
    private PermissionService permissionService;

    @GetMapping("/list")
    public ResponseUtil getAllPermissions() {
        return permissionService.getAllPermissions();
    }

    /**
     * 获取角色当前权限
     */
    @GetMapping("/role/{roleId}")
    public ResponseUtil getRolePermissions(@PathVariable Integer roleId) {
        return permissionService.getPermissionsByRoleId(roleId);
    }

    /**
     * 为角色分配权限
     */
    @PostMapping("/assign")
    public ResponseUtil assignPermissions(@RequestBody Map<String, Object> params) {
        try {
            Integer roleId = Integer.valueOf(params.get("roleId").toString());
            @SuppressWarnings("unchecked")
            List<Integer> permissionIds = (List<Integer>) params.get("permissionIds");

            return permissionService.assignPermissionsToRole(roleId, permissionIds);
        } catch (Exception e) {
            return ResponseUtil.get(ResponseEnum.FAIL,"参数解析失败");
        }
    }

    /**
     * 检查权限
     */
    @GetMapping("/check")
    public ResponseUtil checkPermission(@RequestParam Integer staffId,
                                        @RequestParam String permCode) {
        return permissionService.checkUserPermission(staffId, permCode);
    }
}
